"""
flask 路由、反向代理知识
模板相关知识，模板继承、
"""


from flask import Flask, Response, make_response, request, render_template, redirect, url_for

import settings

app = Flask(__name__)
print(app.config)
app.config.from_object(settings)


# 路由
@app.route('/', methods=['GET', 'POST'])
def index():
    return render_template('index.html')


# 请求对象 请求行+请求头+请求体
# 视图函数-传递参数
@app.route('/city/<string:name>')
def get_city(name):
    return '%s' % name


@app.route('/city/<int:num>')
def get_city1(num):
    return '%d' % num


# return 默认是response 对象 响应行+响应头+响应体
@app.route('/city/1')
def get_cityid():
    return Response('<h1>成都</h1>')  # content_type:text/html,  content_type:application/json charset:utf-8


@app.route('/city')
def city():
    print(request.headers)
    content = '''
    sorry to not get !
    '''
    response = make_response(content)
    response.headers['11111'] = 'ad22222ana'
    print(response.content_encoding)
    # response.set_cookie('tianchuan', '13234')
    return response


# 注册案列
# 知识1：get 请求用arg获取参数，参数在请求地址中显示，post请求用form获取参数，参数在body中
# http://127.0.0.1:5000/register2?username=tianchuan&password=121323
# 知识2：form 表单提交参数，request 获取参数值
@app.route('/register', methods=['GET', ])
def register():
    r = render_template('register_tmp.html')
    return r


users = list()


# 跳转到注册验证
@app.route('/register2', methods=['GET', 'POST'])
def register2():
    if request.method == 'POST':
        name = request.form.get('username')
        password = request.form.get('password')
        repassword = request.form.get('repassword')
        if password == repassword:
            user = {'username': name, 'password': password}
            users.append(user)
            # return '''注册成功！<a href='/'>跳转主页<a/>'''
            return redirect('/')
        else:
            return '两次密码不同'
    return render_template('register_tmp.html')


# 显示注册用户信息
@app.route('/show')
def show():
    names = ['张三', '李四', '王五', '赵六', '吴期', '田串串']
    users = [
        {'userid': '0001', 'name': 'name1', 'phone': '12132434'},
        {'userid': '0002', 'name': 'name2', 'phone': '12132434'},
        {'userid': '0003', 'name': 'name3', 'phone': '132223132434'},
        {'userid': '0004', 'name': 'name4', 'phone': '12132434'}
    ]
    msg = '<h1>i LOVE u !!</h1>'
    # 自定义过滤
    # 1、通过add_templates_filter方式
    filter_msg = 'hi hello world ! flask'
    # 2、通过装饰器方法
    return render_template('show.html', names=names, users=users, msg=msg, filter_msg=filter_msg)


# url_for 路径反向解析,根据endpoint解析url
@app.route('/test', endpoint='test1')
def test():
    url = url_for('test1')
    print(url)
    print(url_for('show'))
    return '测试'


# 自定义过滤器
def replace_hello(value):
    value = str(value).replace('hello', '')
    return value.strip()


app.add_template_filter(replace_hello, 'replace_str')


# 模板过滤
@app.template_filter('reverse_str')
def str_reverse(value: str):
    return value[::-1]


# 模板继承
@app.route('/base')
def base():
    return render_template('base.html')


@app.route('/base/subbase')
def subbase():
    return render_template('subbase.html')


# 宏测试
@app.route('/macro/1')
def my_marco():
    return render_template('macroTest.html')


if __name__ == '__main__':
    print(app.url_map)
    app.run()
